
import cmpe273.classproject.server.model.Person;
import cmpe273.classproject.server.service.MovieServiceImplProxy;
import cmpe273.classproject.server.dto.PersonResponse;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AllSimple.java
 *
 * Created on May 9, 2011, 6:43:03 AM
 */
public class AllSimple extends javax.swing.JFrame {

    /** Creates new form AllSimple */
    public AllSimple() {
        initComponents();
        loadSimpleCustomers();
        loadPremiumCustomers();
    }
    
    public AllSimple(CreateMemb ui) {
         createMemb = ui;
        initComponents();
        loadSimpleCustomers();
        loadPremiumCustomers();
    }
    
    public AllSimple(PremiumMemb ui) {
        premiumMemb = ui;
        initComponents();
        loadSimpleCustomers();
        loadPremiumCustomers();
    }
    
    public AllSimple(SimpleCustomer ui) {
         simpleCustomer = ui;
        initComponents();
        loadSimpleCustomers();
        loadPremiumCustomers();
    }   
    
     public AllSimple(SearchMembers ui) {
         searchMembers = ui;
        initComponents();
        loadSimpleCustomers();
        loadPremiumCustomers();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        add = new javax.swing.JButton();
        delete = new javax.swing.JButton();
        profile = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        profile1 = new javax.swing.JButton();
        search = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("All Simple Members");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "S. No.", "Name", "Membship No.", "City", "State", "Charges Due"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        add.setText("Add New");
        add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addActionPerformed(evt);
            }
        });

        delete.setText("Delete");
        delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteActionPerformed(evt);
            }
        });

        profile.setText("Show Premium Member Details");
        profile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                profileActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("All Premium Members");

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "S. No.", "Name", "Membship No.", "City", "State", "Fee"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTable2);

        profile1.setText("Show Simple Member Details");
        profile1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                profile1ActionPerformed(evt);
            }
        });

        search.setText("Search");
        search.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(373, Short.MAX_VALUE)
                .addComponent(profile, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 231, Short.MAX_VALUE)
                .addComponent(profile1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
            .addGroup(layout.createSequentialGroup()
                .addGap(158, 158, 158)
                .addComponent(add)
                .addGap(18, 18, 18)
                .addComponent(delete)
                .addGap(18, 18, 18)
                .addComponent(search)
                .addContainerGap(175, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {add, delete});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(profile1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(profile)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(add)
                    .addComponent(delete)
                    .addComponent(search))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    public CreateMemb createMemb = null;
    public PremiumMemb premiumMemb = null;
    public SimpleCustomer simpleCustomer = null;
    public SearchMembers searchMembers = null;
    
    
    private void addActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        
        if (createMemb == null)
            {
                createMemb = new CreateMemb(this);
            }
        
        createMemb.setVisible(true);
        this.setVisible(false);   
        
}   
    
    private void deleteActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        
    	if (jTable2.getSelectedRow() < 0 && jTable1.getSelectedRow() < 0)
        {
            JOptionPane.showMessageDialog(this, "Please select member to view member's details.");
            return;
        }
    	
    	if (jTable2.getSelectedRow() >= 0)
        {
    		int iSelectedRow = jTable2.getSelectedRow();
            int memberID = Integer.parseInt(jTable2.getValueAt(iSelectedRow, 0).toString());
            deletePerson(memberID);
        }
    	
    	if (jTable1.getSelectedRow() >= 0)
        {
    		int iSelectedRow = jTable1.getSelectedRow();
            int memberID = Integer.parseInt(jTable1.getValueAt(iSelectedRow, 0).toString());
            deletePerson(memberID);
        }
        
        loadSimpleCustomers();
        loadPremiumCustomers();
        
}   

    private void profileActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        // premium
    	
    	if (jTable2.getSelectedRow() < 0)
        {
            JOptionPane.showMessageDialog(this, "Please select member to view member's details.");
            return;
        }
    	
    	int iSelectedRow = jTable2.getSelectedRow();
        int memberID = Integer.parseInt(jTable2.getValueAt(iSelectedRow, 0).toString());
        
        if (premiumMemb == null)
        {
        	premiumMemb = new PremiumMemb(this);
        }
        
        premiumMemb.memberID = memberID;
        premiumMemb.name = jTable2.getValueAt(iSelectedRow, 1).toString();
        premiumMemb.address = jTable2.getValueAt(iSelectedRow, 4).toString();
        premiumMemb.balance = jTable2.getValueAt(iSelectedRow, 9).toString();
        premiumMemb.loadMember();
        
        
        
        premiumMemb.setVisible(true);
        this.setVisible(false);   
                 
    }

    private void profile1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        // simple
    	
    	if (jTable1.getSelectedRow() < 0)
        {
            JOptionPane.showMessageDialog(this, "Please select member to view member's details.");
            return;
        }
    	
    	int iSelectedRow = jTable1.getSelectedRow();
        int memberID = Integer.parseInt(jTable1.getValueAt(iSelectedRow, 0).toString());
        
        if (simpleCustomer == null)
        {
            simpleCustomer = new SimpleCustomer(this);
        }
        
        simpleCustomer.memberID = memberID;
        simpleCustomer.name = jTable1.getValueAt(iSelectedRow, 1).toString();
        simpleCustomer.address = jTable1.getValueAt(iSelectedRow, 4).toString();
        simpleCustomer.balance = jTable1.getValueAt(iSelectedRow, 9).toString();
        simpleCustomer.loadMember();
        
        
        
        simpleCustomer.setVisible(true);
        this.setVisible(false);   
    }

    private void searchActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        if (searchMembers == null)
            {
                searchMembers = new SearchMembers(this);
            }
        
        searchMembers.setVisible(true);
        this.setVisible(false);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AllSimple().setVisible(true);
            }
        });
    }
    
    public void loadSimpleCustomers()
    {
        String memberId, fname, lname, email, address, city, state, zipcode, phone, balance ;
        String[] columns = new String[]{"memberId", "first name", "last name", "email", "address", "city", "state", "zipcode", "phone", "balance"};        
        DefaultTableModel modal = new DefaultTableModel(new String[][]{}, columns);
        
                   
            List<Person> persons = getAllSimpleCustomers();
            
            if (persons != null && persons.size() > 0)
            {
                                
                for(int i=0; i< persons.size(); i++)
                {
                    Person person = persons.get(i);
                    memberId = String.valueOf(person.getMemberId());
                    fname = person.getFirstName();
                    lname = person.getLastName();
                    email = person.getEmailId();
                    address = person.getAddress();
                    city = person.getCity();
                    state = person.getState();
                    zipcode = person.getZipCode();
                    phone = String.valueOf(person.getPhoneNo());
                    balance = String.valueOf(person.getBalance());

                    modal.addRow(new String[]{memberId, fname, lname, email, address, city, state, zipcode, phone, balance});
                }
            }
        
        
        jTable1.setModel(modal);
        
    }
    
    public void loadPremiumCustomers()
    {
        String memberId, fname, lname, email, address, city, state, zipcode, phone, balance ;
        String[] columns = new String[]{"memberId", "first name", "last name", "email", "address", "city", "state", "zipcode", "phone", "balance"};        
        DefaultTableModel modal = new DefaultTableModel(new String[][]{}, columns);
        
                   
            List<Person> persons = getAllPremiumCustomers();
            
            if (persons != null && persons.size() > 0)
            {
                                
                for(int i=0; i< persons.size(); i++)
                {
                    Person person = persons.get(i);
                    memberId = String.valueOf(person.getMemberId());
                    fname = person.getFirstName();
                    lname = person.getLastName();
                    email = person.getEmailId();
                    address = person.getAddress();
                    city = person.getCity();
                    state = person.getState();
                    zipcode = person.getZipCode();
                    phone = String.valueOf(person.getPhoneNo());
                    balance = String.valueOf(person.getBalance());

                    modal.addRow(new String[]{memberId, fname, lname, email, address, city, state, zipcode, phone, balance});
                }
            }
        
        
        jTable2.setModel(modal);
        
    }
    
    // Variables declaration - do not modify
    private javax.swing.JButton add;
    private javax.swing.JButton delete;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JButton profile;
    private javax.swing.JButton profile1;
    private javax.swing.JButton search;
    // End of variables declaration

    private List<Person> getAllSimpleCustomers()
    {
    	try
    	{
    		MovieServiceImplProxy proxy = new MovieServiceImplProxy();
    		Person[] persons = proxy.getAllSimpleCustomers();
    		List<Person> lst = Arrays.asList(persons);
    		return lst;
    	}
    	catch(Exception ex)
    	{
    		ex.printStackTrace();
    	}
    	
    	return null;
    }
    
    private List<Person> getAllPremiumCustomers()
    {
    	try
    	{
    		MovieServiceImplProxy proxy = new MovieServiceImplProxy();
    		Person[] persons = proxy.getAllPremiumMembers();
    		List<Person> lst = Arrays.asList(persons);
    		return lst;
    	}
    	catch(Exception ex)
    	{
    		ex.printStackTrace();
    	}
    	
    	return null;
    }
    
    private PersonResponse deletePerson(int memberID)
    {
    	try
    	{
    		Person person = new Person();
    		person.setMemberId(memberID);
    		
    		MovieServiceImplProxy proxy = new MovieServiceImplProxy();
    		PersonResponse personResponse = proxy.deletePerson(memberID); 
    		if (personResponse.getError() < 0)
    		{
    			JOptionPane.showMessageDialog(this, "Cannot delete member, since member has movies issued.");
    		}
    		return personResponse;
    	}
    	catch(Exception ex)
    	{
    		ex.printStackTrace();
    	}
    	
    	return null;
    }
    

    

   

   

   
       
}

